#ifndef SCANNER_H
#define SCANNER_H

#include <QThread>
#include "database.h"
#include <QString>
#include <QMutex>
#include <QWaitCondition>
#include <QStringList>
#include <QFutureSynchronizer>
#include "mediaitemimpl.h"

class Scanner : public QThread
{
    Q_OBJECT
public:
    Scanner();
    virtual ~Scanner();
    void scan(Database * database, const QString & path);
    static bool update(MediaItemImpl::Metadata & metadata);

signals:
    void status(const QString & status);
    void add(MediaItemPtr item);
    void remove(MediaItemPtr item);
    void update(MediaItemPtr item, const MediaItemImpl::Metadata & metadata);

private:
    struct QueueItem {
        QString path;
        QMap<QString, QPair<MediaItemPtr, MediaItemImpl::Metadata> > data;
    };

    virtual void run();
    void collect(class ImageLoader & loader, const QString & path, QSet<QString> & files);
    void scan(const QueueItem & item);

    static void loadRawMetadata(MediaItemImpl::Metadata & metadata);
    static void loadXmpMetadata(MediaItemImpl::Metadata & metadata);

private:
    bool m_stop;
    QMutex m_mutex;
    QWaitCondition m_cond;

    QList<QueueItem> m_queue;
    static QSet<QString> s_rawformats;
};

#endif // SCANNER_H
